/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.commons.util.MatcherFormatUtil;
/**
 * 说明：
 * 商品详细公共model
 * @Author：李智深
 * @Date：2012-03-16
 */
public class OrderDetailModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private List<TBillDetail> tBillDetailList=new ArrayList<TBillDetail>();
	private Vector<String> columns = new Vector<String>();
	
	public OrderDetailModel(){
		columns.add("商品名称");
		columns.add("商品条形码");
		columns.add("商品数量");
		columns.add("商品价格");
		columns.add("合计");
	}
	
	//获得行数
	@Override
	public int getRowCount(){
		return tBillDetailList.size();
	}
	
	//获得列数
	@Override
	public int getColumnCount(){
		return columns.size();
	}
	//获取列名
	@Override
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
//	//获取列对应的类型
//	public Class getColumnClass(int column){
//		return Object.class;
//	}
	//获得单元格的值
	@Override
	public Object getValueAt(int row,int column){
		TBillDetail tBillDetail = tBillDetailList.get(row);
		if(tBillDetail!=null){
			TCommodity commodity=tBillDetail.getCommodity();
			switch (column) {
				case 0:
					if(commodity!=null){
						return commodity.getName();
					}
					return "";
				case 1:
					if(commodity!=null){
						return commodity.getBarcode();
					}
					return "";
				case 2:
					return tBillDetail.getNum();
				case 3:
					return tBillDetail.getPrice();
				case 4:
					return tBillDetail.getNum()*tBillDetail.getPrice();
			}
		}
		return null;
	}
	
	//设置单元格的值
	@Override
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		TBillDetail tBillDetail = tBillDetailList.get(rowIndex);
		if(tBillDetail!=null&&aValue!=null){
			switch (columnIndex) {
				case 0:
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tBillDetail.setCommodityID(comm.getCommodityID());
						tBillDetail.setPrice(comm.getPrice());
						tBillDetail.setCommodity(comm);
						this.fireTableRowsUpdated(rowIndex, rowIndex);
					}
					return;
				case 1:
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tBillDetail.setCommodityID(comm.getCommodityID());
						tBillDetail.setPrice(comm.getPrice());
						tBillDetail.setCommodity(comm);
					}
					return;
				case 2:
					if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherNumberScheme(aValue.toString())&&aValue.toString().length()<10){
						tBillDetail.setNum(Integer.parseInt(aValue.toString()));
						fireTableRowsInserted(rowIndex, rowIndex);
					}
					return;
				case 3:
					if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherPriceScheme(aValue.toString())){
						tBillDetail.setPrice(Double.parseDouble(aValue.toString()));
						fireTableRowsInserted(rowIndex, rowIndex);
					}
					return;
				case 4:
			}
		}
		super.setValueAt(aValue, rowIndex, columnIndex);
	}
	
	public void addBillDetail(TBillDetail tBillDetail) {
		tBillDetailList.add(tBillDetail);
		fireTableRowsInserted(tBillDetailList.size()-1, tBillDetailList.size()-1);
	}
	
	public void setBillDetailList(List<TBillDetail> billDetailList){
		this.tBillDetailList=billDetailList;
	}
}
